package database.local;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import server.SaveZoneHttpConnection;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.util.Log;
import app.ui.R;

import com.google.android.maps.GeoPoint;

public class Utils  {
	
	public static ArrayList<String> bmp_to_weekdays_list(int days_bmp) {
		ArrayList<String> result = new ArrayList<String>();
		
		if ((QueryData.ALL_DAYS & days_bmp) == QueryData.ALL_DAYS) {
			result.add("Everyday");
			return result;
		}
		
		if ((QueryData.SUNDAY & days_bmp) != 0) result.add("Sunday");
		if ((QueryData.MONDAY & days_bmp) != 0) result.add("Monday");
		if ((QueryData.TUESDAY & days_bmp) != 0) result.add("Tuesday");
		if ((QueryData.WEDNESDAY & days_bmp) != 0) result.add("Wedensday");
		if ((QueryData.THURSDAY & days_bmp) != 0) result.add("Thursday");
		if ((QueryData.FRIDAY & days_bmp) != 0) result.add("Friday");
		if ((QueryData.SATURDAY & days_bmp) != 0) result.add("Saturday");
		return result;
	}
	
	public static int bmp_next_day(int bmp) {
		bmp = bmp << 1;
		if ((bmp & 0x80) != 0) return ((0x1 | bmp) & QueryData.ALL_DAYS);
		return bmp;
	}
	
	public static String email_to_name(String email) {
		String name = "";
		int index = email.indexOf('@');
		if (index == -1) return name;
		name = email.substring(0, index);
		return name;
	}
	
	/**
	 * 
	 * @param start 
	 * @param delta
	 * @return the hour in the next day
	 */
	public static int hourAfterHourAdd(int start,int delta ){
		int result = start + delta;
		if (result <= 23)
			return result;
		return result - 24;
	}
	
	/**
	 * 
	 * @param start
	 * @param delta
	 * @return 1 - for next day, 0 to stay in current day
	 */
	public static int dayAfterHourAdd(int start, int delta){
		int result = start + delta;
		if (result <= 23)
			return 0;
		return 1;
	}
	
	/**
	 * 
	 * @param start 
	 * @param delta
	 * @return the min in the next day
	 */
	public static int minAfterMinAdd(int start,int delta ){
		int result = start + delta;
		if (result <= 59)
			return result;
		return result - 60;
	}

	/**
	 * 
	 * @param start 
	 * @param delta
	 * @return 1 - for next hour, 0 to stay in current hour
	 */
	public static int hourAfterMinAdd(int start, int delta){
		int result = start + delta;
		if (result <= 59)
			return 0;
		return 1;
	}

	/**
	 * 
	 * @param time - in format : "hh:dd"
	 * @param delta_time - in format : "hh:dd"
	 * @return adding the time smartly : "hh:dd" or "Anytime" if "all" is on for one of the fields
	 */
	public static String computeTimeFromStrings(String time, String delta_time) {
		if (time.equalsIgnoreCase("all") || delta_time.equalsIgnoreCase("all") )
			return "Anytime";
		
		String[] currTime = time.split(":", 2);
		if (currTime[0].equalsIgnoreCase("") || currTime[1].equalsIgnoreCase("") )
			return "Anytime";
		int currHour = Integer.valueOf(currTime[0]);
		int currMin = Integer.valueOf(currTime[1]);
		
		int deltaHour = Integer.valueOf(delta_time);
		
		int resultHour = hourAfterHourAdd(currHour, deltaHour);
		
		return pad(resultHour) + ":" + pad(currMin);
	}
	
	 /**
	  * 
	  * @param c - an hour or minute value
	  * @return the number, as string, if (c >= 0 && c <=9) add zero in front it. 
	  * e.g if c = 5 return "05" 
	  * if c = 11 return "11"
	  */
		public static String pad(int c) {
		    if (c >= 10)
		        return String.valueOf(c);
		    else
		        return "0" + String.valueOf(c);
		}

	
	/**
	 * The method will return a list of businesses sorted by Rating
	 * @param lst of business
	 * @return List of businesses sorted by Rating
	 */
		public static List<Business> getListbyRating(List<Business> lst){
			
			if (lst != null){
			Collections.sort(lst);
			return lst;
			}
			else
			{
				return null;
			}
			
		}
		/**
		 * The method will return a list of businesses sorted by Name
		 * @param lst of business
		 * @return List of businesses sorted by Rating
		 */
			
		public static List<Business> getListbyName(List<Business> lst){
			
			Collections.sort(lst, new Comparator<Business>() {
							    public int compare(Business e1, Business e2) {
							        return e1.getName().compareTo(e2.getName());
			                         }
							    }
							    );			
			return lst;			
		}
		
		/**
		 * 
		 * @param calc business distance from cur geo_point
		 * in order to get  the distance use business.getDistance
		 */
		public static void calcDistance(List<Business> lst, GeoPoint cur_geo_point) {			
			if (cur_geo_point == null){				
				return;
			}
			Log.i("Util", "ClacDistance is here " +  cur_geo_point.getLatitudeE6() +","+ cur_geo_point.getLongitudeE6());
			for (Business business: lst) {
				if (business.getGeoPoint() == null) {
					business.setDistance(Integer.MAX_VALUE);
				} else {
					try {
						float[] results = new float[3];
						Location.distanceBetween(((double)cur_geo_point.getLatitudeE6()) / 1E6,((double) cur_geo_point.getLongitudeE6())/ 1E6,((double) business.getLatitude())/ 1E6, ((double)business.getLongitude()) / 1E6, results);
						business.setDistance((int)results[0]);
					}
					catch (IllegalArgumentException e) {
						business.setDistance(Integer.MAX_VALUE);
					}
					
				}
			}
			
		}
		
		/**
		 * The method will return a list of businesses sorted by Name
		 * @param lst of business
		 * @return List of businesses sorted by Rating
		 */
			
		public static List<Business> getListbyDistance(List<Business> lst){
			
			Collections.sort(lst, new Comparator<Business>() {
			    public int compare(Business e1, Business e2) {
			        if (e1.getDistance() > e2.getDistance()) return 1;
			        if (e1.getDistance() < e2.getDistance()) return -1;
			        return 0;
			    }
			}
					);			
			return lst;			
		}
		
		
		public static String getDistanceStr(int distance_in_meter) {
			String res = "";
			if (distance_in_meter == 0) return res;
			if (distance_in_meter < 1000) {
				res += String.valueOf(distance_in_meter);
				res += " Meter";
			} else {
				/*round two left digits*/
				distance_in_meter -= (distance_in_meter % 100);
				double  distance_in_km = ((double) distance_in_meter)/1000;
				res += String.valueOf(distance_in_km);
				res += " KM";
			}
			
			return res;
		}
		
		public static void sendFacebookLikes(User user, List<String> likes){
			SaveZoneHttpConnection.sendFacebookLikes(user, likes);	
		}
}
